﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itfort;
using MySoft.Data;

namespace WanPiJie
{
	/// <summary>
	/// Baby。
	/// </summary>
	public class BabyService : IBabyService
	{
		private DbSession dbSession = new DbSession("Ehr");

		/// <summary>
		/// 添加Baby。
		/// </summary>
		/// <param name="userId">用户编号。</param>
		/// <param name="name">宝宝名字。</param>
		/// <param name="birthday">宝宝生日。</param>
		public Baby AddBaby(int userId, string name, DateTime birthday, string gender)
		{
			_Baby _baby = new _Baby();
			_baby.AddTime = DateTime.Now;
			_baby.UpdateTime = _baby.AddTime;
			_baby.UserId = userId;
			_baby.Name = name;
			_baby.Birthday = birthday;
			_baby.Gender = gender;

			dbSession.Save(_baby);
			return GetBaby(_baby.Id);
		}

		/// <summary>
		/// 获取Baby。
		/// </summary>
		/// <param name="id">宝宝编号。</param>
		/// <returns></returns>
		public Baby GetBaby(int id)
		{
			return dbSession.Single<_Baby>(_Baby._.Id == id)
				.Convert2<Baby>();
		}

		/// <summary>
		/// 获取Baby。
		/// </summary>
		/// <param name="userId">用户名。</param>
		/// <returns></returns>
		public List<Baby> GetBabies(int userId)
		{
			return dbSession.From<_Baby>()
				.Where(_Baby._.UserId == userId)
				.ToList()
				.Convert2<Baby>();
		}

		/// <summary>
		/// 修改宝宝姓名。
		/// </summary>
		/// <param name="id">宝宝编号。</param>
		/// <param name="name">宝宝姓名。</param>
		public void UpdateName(int id, string name)
		{
			dbSession.Update<_Baby>(new FieldValue(_Baby._.Name, name), _Baby._.Id == id);
		}

		/// <summary>
		/// 修改宝宝头像。
		/// </summary>
		/// <param name="id">宝宝编号。</param>
		/// <param name="avatar">宝宝头像。</param>
		public void UpdateAvatar(int id, string avatar)
		{
			dbSession.Update<_Baby>(_Baby._.Avatar, avatar, _Baby._.Id == id);
		}
	}
}
